package cn.kys.modules.system.service.impl;


import cn.kys.modules.system.repository.ToolsResposity;
import cn.kys.modules.system.service.FormContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.util.*;

@Service
public class FormContentServiceImpl implements FormContentService {
    @Autowired
    private ToolsResposity toolsResposity;

    @Override
    public Map addFormContent(String name, String cname,String type,String enumvalue) {
        Map returnMap = new HashMap();
        String sql = "SELECT * FROM public.t_formcontent where name = '" + name + "'";
        Map formContentMap = toolsResposity.exeSql(sql);
        if(formContentMap==null){

            String sql_insert = "INSERT INTO public.t_formcontent(name,cname,type,enum) VALUES ('" + name + "','" + cname +"','"+type+ "','"+enumvalue+"');";
            toolsResposity.exeSql(sql_insert);
            returnMap.put("success",true);

        }else {
            returnMap.put("success",false);
            returnMap.put("msg","表单项目已经存在");
        }

        return returnMap;
    }


    @Override
    public void addMultiFormContents(String jsonStr) {

    }



    @Override
    public Map editFormContent(String id, String name, String cname, String type,String enumvalue) {

        String sql = "UPDATE t_formcontent SET cname = '" + cname + "',name='" + name + "',type='" + type +"',enum='"+enumvalue+"' WHERE id ='" + id + "'";
        toolsResposity.exeSql(sql);
        return null;
    }

    @Override
    public List getAllFormcontents() {
        String sql = "SELECT * FROM public.t_formcontent";
        return toolsResposity.exeSqlList(sql);
    }

    @Override
    public List getFormContentById(String fieldId) {
        return null;
    }

    @Override
    public Map importFormContentsByExcel(MultipartFile file, int layerid) {
        return null;
    }

    @Override
    public Map delFormContentById(String id) {
        Map returnMap = new HashMap();

        String sql = "select * from t_form_content where formcontentid in (" + id + ")";
        List formContentMap = toolsResposity.exeSqlList(sql);
        if (formContentMap.size() > 0) {
            //不能删
            returnMap.put("deleted", false);
            returnMap.put("msg", "表单表中使用了该图层，不能删除");
        } else {
            //可以删除
            String sql_delete = "delete from t_formcontent where id in (" + id + ")";
            toolsResposity.exeSql(sql_delete);
            returnMap.put("deleted", true);
        }

        return returnMap;
    }

    @Override
    public List getAllContentTypes() {
        String sql = "SELECT * FROM public.t_contenttype";
        return toolsResposity.exeSqlList(sql);
    }
}

